<?php
namespace Common\ORG\Qy;
// send message
use \Common\ORG\Tools\Http;

class Message extends Base{
    const URL_MSGSEND =  'https://qyapi.weixin.qq.com/cgi-bin/message/send';
    const URL_UPLOADFILE = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload';

    //初始化函数
    public function __construct($token)
    {
        $this->_token = $token;
    }

    // file upload
    public function uploadfile($msgtype,$file){
        $ret = $this->m_ret;
        $force_encode = false;
        $param = array();
        $param['access_token'] = $this->_token;
        $param['type'] = $msgtype;
        $url = self::URL_UPLOADFILE.'?'.http_build_query( $param );
        $data = array('media'=>'@'.$file);
        //$params = $this->_param($data);     
        $res = Http::post($url, $data,$force_encode);
        $crackData = $this->crackRes( $res );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
        $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
        return $ret;       
    }

    // file mpnews
    public function uploadmpnews($touser = null,$toparty = null,$totag = null,$msgtype,$agentid,$text,$safe = null){
        $ret = $this->m_ret;
        $url = self::URL_MSGSEND.'?access_token='.urlencode($this->_token); 
        $data = array('msgtype'=>$msgtype,'agentid'=>$agentid,'mpnews'=>$text);
        if ($touser)
            $data['touser'] = $touser;
        if ($toparty)
            $data['toparty'] = $toparty;
        if ($totag)
            $data['totag'] = $totag;
        if ($safe){
            $data['safe'] = $safe;            
        }else{
            $data['safe'] = 0;
        }
        $param = $this->_param($data);           
        $res = Http::post($url, $param);
        $crackData = $this->crackRes( $res );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
        $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
        return $ret;       
    }

    // upload text
    public function uploadtext($touser = null,$toparty = null,$totag = null,$msgtype,$agentid,$text,$safe = null){
        $ret = $this->m_ret;
        $url = self::URL_MSGSEND.'?access_token='.urlencode($this->_token); 
        $data = array('msgtype'=>$msgtype,'agentid'=>$agentid,'text'=>$text);
        if ($touser)
            $data['touser'] = $touser;
        if ($toparty)
            $data['toparty'] = $toparty;
        if ($totag)
            $data['totag'] = $totag;
        if ($safe){
            $data['safe'] = $safe;            
        }else{
            $data['safe'] = 0;
        }
        $param = $this->_param($data);           
        $res = Http::post($url, $param);
        $crackData = $this->crackRes( $res );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
        $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
        return $ret; 
    }

    public function uploadfilemsg($touser = null,$toparty = null,$totag = null,$msgtype,$agentid,$media,$safe = null){
        $ret = $this->m_ret;
        $url = self::URL_MSGSEND.'?access_token='.urlencode($this->_token); 
        if($msgtype == 'file'){
            $data = array('msgtype'=>$msgtype,'agentid'=>$agentid,'file'=>$media);
        }else{
            $data = array('msgtype'=>$msgtype,'agentid'=>$agentid,'image'=>$media);
        }
        if ($touser)
            $data['touser'] = $touser;
        if ($toparty)
            $data['toparty'] = $toparty;
        if ($totag)
            $data['totag'] = $totag;
        if ($safe){
            $data['safe'] = $safe;            
        }else{
            $data['safe'] = 0;
        }
        $param = $this->_param($data);           
        $res = Http::post($url, $param);
        $crackData = $this->crackRes( $res );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
        $ret['data'] = isset($crackData['data']) ? $crackData['data']:false;
        return $ret; 
    }

}
?>